package com.qf.purchase.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.purchase.po.PurchaseOrder;
import com.qf.purchase.po.PurchaseOrderDetails;
import com.qf.purchase.service.PurchaseOrderDetailsService;
import com.qf.purchase.service.PurchaseOrderService;
import com.qf.purchase.utils.LayUIOperate;
import com.qf.purchase.utils.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/PurchaseOrderDetails")
public class PurchaseOrderDetailsController {

    @Resource
    private PurchaseOrderDetailsService purchaseOrderDetailsService;
    @Resource
    private PurchaseOrderService purchaseOrderService;

    @RequestMapping("/getPurchaseOrderDetailsList")
    @ResponseBody
    public TableData getAllPersonList(HttpServletRequest request) {
        int pageSize =5;
        int pageNumber = 5;

        String number = request.getParameter("number");

        Map params = new HashMap();
        params.put("number", number);

        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<PurchaseOrderDetails> data = purchaseOrderDetailsService.getPurchaseOrderDetailsByNumber(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        return tableData;
    }

    @RequestMapping("/updatePurchaseOrderDetails")
    @ResponseBody
    public LayUIOperate updatePurchaseOrderDetails(@RequestBody JSONObject object){
        LayUIOperate operate=new LayUIOperate();
        String number = object.getString("number");
        String amount = object.getString("amount");

        String editName = object.getString("editName");
        String editType = object.getString("editType");
        String editMode = object.getString("editMode");
        String editManu = object.getString("editManu");
        String inputNum = object.getString("inputNum");
        String inputUnits = object.getString("inputUnits");
        String inputPrice = object.getString("inputPrice");
        String inputAmounts = object.getString("inputAmounts");
        String orderId = object.getString("orderId");;

        PurchaseOrder purchaseOrder = purchaseOrderService.findPurchaseOrderByNumber(number);
        purchaseOrder.setAmount(amount);
        PurchaseOrderDetails purchaseOrderDetails = new PurchaseOrderDetails(number,orderId,
                editName,editType,editMode,editManu,inputNum,inputUnits,inputPrice,inputAmounts);

        boolean f1= purchaseOrderService.updatePurchaseOrder(purchaseOrder);
        boolean f2= purchaseOrderDetailsService.updatePurchaseOrderDetails(purchaseOrderDetails);
        if(f1&&f2){
            operate.setSuccess(true);
            operate.setMessage("采购单修改成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("采购单修改失败");
        }
        return operate;
    }

    @RequestMapping("/delPurchaseOrderDetails")
    @ResponseBody
    public LayUIOperate delPurchaseOrderDetails(HttpServletRequest request){
        LayUIOperate operate=new LayUIOperate();
        String number = request.getParameter("number");
        String orderId = request.getParameter("orderId");
        String amounts = request.getParameter("amounts");

        PurchaseOrder purchaseOrder = purchaseOrderService.findPurchaseOrderByNumber(number);
        String amount =String.valueOf(Integer.parseInt(purchaseOrder.getAmount())-Integer.parseInt(amounts));
        purchaseOrder.setAmount(amount);
        Map params = new HashMap();
        params.put("number", number);
        params.put("orderId", orderId);
        boolean f1 = purchaseOrderService.updatePurchaseOrder(purchaseOrder);
        boolean f2 = purchaseOrderDetailsService.delPurchaseOrderDetails(params);
        if(f1&f2){
            operate.setSuccess(true);
            operate.setMessage("采购单删除成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("采购单删除失败");
        }
        return operate;
    }
}
